package com.zgscwjm.taobaphelper.data;

import com.zgscwjm.taobaphelper.exutils.LsfbLog;

import org.apache.commons.codec.binary.Hex;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import static javax.crypto.Cipher.DECRYPT_MODE;
import static javax.crypto.Cipher.ENCRYPT_MODE;

/**
 * Created by zgscwjm on 17-3-2.
 * 对应原来 data.c
 */

public class PrivateKey {

    /**
     * 对应原来的a
     */
    static final char[] hexstr = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static final EncryptionType encryptionTYpe = EncryptionType.MD5;

    /**
     * 使字符串长度为16位,不足的0补齐,多余截取
     * @param content
     * @return
     */
    private static String subset16str(String content){
        int i;
        String stringBuilder;
        int length = content.length();
        //不足16位用0补齐
        if (length<16){
            int i2 = 16 - length;
            StringBuilder stringBuilder2 = new StringBuilder(content);
            for (i = 0;i<i2;i++){
                stringBuilder2.append('0');
            }

            stringBuilder = stringBuilder2.toString();
        }else{
            stringBuilder = content;
        }

        if (length<=16){
            return stringBuilder;
        }

        StringBuffer stringBuffer3 = new StringBuffer(stringBuilder);
        for (i = length;i>16;i--){
            stringBuffer3.deleteCharAt(i-1);
        }
        return stringBuffer3.toString();
    }

    /**
     * 加密
     * @param inputStr
     * @param secretkey
     * @return
     * @throws dataException
     */
    public static String EncryptPrivate(String inputStr,String secretkey) throws dataException {
        try{
            // secretKey的实现 秘密（对称）密钥
            Key secretKeySpec = new SecretKeySpec(subset16str(secretkey).getBytes(),"AES");
            // 创建密码器
            Cipher instance = Cipher.getInstance("AES");

            //初始化 加密
            instance.init(ENCRYPT_MODE,secretKeySpec);

            // 运行
            return String.valueOf(Hex.encodeHex(instance.doFinal(inputStr.getBytes())));
        }catch (Exception e){
            throw  new dataException(500,e.getMessage());
        }
    }

    /**
     * 解密
     * @param str
     * @param secretkey
     * @return
     * @throws dataException
     */
    public static String DecryptPrivate(String str,String secretkey) throws dataException{
        try{
            String a = subset16str(secretkey);
            LsfbLog.e("value:[" + str + "] private key:" + a + " length:[" + a.length() + "]");

            Key secretKeySpec = new SecretKeySpec(a.getBytes(), "AES");
            // 创建密码器
            Cipher instance = Cipher.getInstance("AES");
            // 初始化  解密
            instance.init(DECRYPT_MODE, secretKeySpec);
            //运行
            return new String(instance.doFinal(DecodeHex.getDecodeHex(str)));
        }catch (Exception e){
            e.printStackTrace();
            throw new dataException(500, e.getMessage());
        }
    }

}
